import ShazamHash.ShazamHash;
import com.sun.media.sound.FFT;

import java.util.ArrayList;
import java.math.*;

/**
 * Created by Administrator on 2016/11/16.
 */
public class Fingerprint {

    // DO NOT write FFT.WINDOW_SIZE / 44100, it equals to 0 forever!!
    public static final double scaling =(4096.0 )/ 44100.0;

    public static final int N = 3;

    private ArrayList<int[]> constel_data = new ArrayList<>();
    private int id;

    /**
     * For songs about to add into DB
     * @param id
     */
    public Fingerprint(int id) {
        this.id = id;
    }

    /**
     * For songs about to be searched
     */
    public Fingerprint() {
        this.id = -1;
    }

    /**
     * Append a column of frequency peaks to the constellation map.
     * A frequency peak is a frequency value whose amplitude is the highest among
     * all frequencies in a frequency interval.
     *
     * @param freqDomain The frequency domain generated by FFT.
     */
    public final int[] RANGE = new int[] { 110, 220, 440, 880, 1760 };
    public int getIndex(int freq) {
        int i = 0;
        while (RANGE[i] < freq)
            i++;
        return i;
    }
    public void append(double[] freqDomain) {
        int[] freqPeaks = new int[N];
        double[] highscore=new double[freqDomain.length];
        double[] points=new double[freqDomain.length];
        for(int i=0;i<freqDomain.length;i++)
        {
            highscore[i]=0;points[i]=0;
        }
            for(int freq=0;freq<1760;freq++)
            {
                double mag=Math.log(Math.abs(freqDomain[freq])+1);
                int index=getIndex(freq);
                if(mag>highscore[freq])
                {
                    highscore[freq]=mag;
                    points[freq]=freq;
                }
            }


        /**
         * TODO: Either find N frequencies with the highest amplitude(energy),
         * or find the frequency with the max energy within each interval.
         */

        constel_data.add(freqPeaks);
    }

    /**
     * Generate fingerprints using Combinational Hash.
     * For each frequency peak, generate 6 fingerprints with its 6 successors.
     *
     * @return
     */
    public ArrayList< ShazamHash> combineHash() {
        if (constel_data.size() < 3)
            throw new RuntimeException("Too few frequency peaks");
        ArrayList< ShazamHash> hashes = new ArrayList<>();
        for (int i = 0; i < constel_data.size() - 2; ++i) {
            for (int k = 0; k <551; ++k) {

                // "Combine" with all peak frequencies inside its next two frames.
                for (int j = 1; j <= 2; ++j) {
                    for (int kk = 1; kk < 1760; ++kk) {
                        ShazamHash hash = new ShazamHash();
                        hash.f1 = (short) constel_data.get(i)[k];
                        hash.f2 = (short) constel_data.get(i + j)[kk];
                        hash.dt = (short) j;
                        hash.offset = i;
                        hash.id = id;
                        hashes.add(hash);
                    }
                }
            }
        }
        return hashes;
    }
}

